// 格式化日期：yyyy-MM-dd HH:mm:ss
export const formatDate = (curDate) => {
	let year = curDate.getFullYear();
	let month =
		curDate.getMonth() + 1 < 10
			? `0${curDate.getMonth() + 1}`
			: curDate.getMonth() + 1;
	let day =
		curDate.getDate() < 10 ? "0" + curDate.getDate() : curDate.getDate();
	let hour = curDate.getHours();
	let minute =
		curDate.getMinutes() < 10
			? "0" + curDate.getMinutes()
			: curDate.getMinutes();
	let second =
		curDate.getSeconds() < 10
			? `0${curDate.getSeconds()}`
			: curDate.getSeconds();
	return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};

// 评论时间转化
export const commentTimeFormat = (time: string): string => {
	let result: string = "";
	const minute: number = 60 * 1000;
	const hour: number = minute * 60;
	const day: number = hour * 24;
	const week: number = day * 7;
	const partMonth: number = day * 15;
	const month: number = day * 30;
	const timeStamp: number = new Date(time).getTime();
	const curTime: string = formatDate(new Date());
	const curTimeStamp: number = new Date(curTime).getTime();

	const diff: number = curTimeStamp - timeStamp;
	if (diff < 0) {
		return result;
	}
	const minC: number = diff / minute;
	const hourC: number = diff / hour;
	const dayC: number = diff / day;
	const weekC: number = diff / week;
	const monthC: number = diff / month;

	switch (true) {
		case monthC >= 1 && monthC <= 3:
			result = `${parseInt(monthC)}月前`;
			break;
		case weekC >= 1 && weekC <= 3:
			result = `${parseInt(weekC)}周前`;
			break;
		case dayC >= 1 && dayC <= 6:
			result = `${parseInt(dayC)}天前`;
			break;
		case hourC >= 1 && hourC <= 23:
			result = `${parseInt(hourC)}小时前`;
			break;
		case minC >= 1 && minC <= 59:
			result = `${parseInt(minC)}分钟前`;
			break;
		case diff >= 0 && diff <= minute:
			result = "刚刚";
			break;
		default:
			result = time;
    }
    console.log(result);
    
	return result;
};
